clear;


% I) Read in data:
%%%%%%%%%%%%%%%%%%%%%%%%%
% 1994 - 2016
mat1994 = readmatrix('/home/Output/Table-3-data.xlsx','Sheet','1994-2016','Range','B2:K93');
%
% 2001 - 2016 (this includes time series for chain length).
mat2001 = readmatrix('/home/Output/Table-3-data.xlsx','Sheet','2001-2016','Range','B2:L62');



%
% II) Estimate 1994-2016.
%%%%%%%%%%%%%%%%%%%%%%%%%
%
% II.A) Format data:
date   = mat1994(:,1);
p_data = mat1994(:,2);
u_data = mat1994(:,3);
f_data = mat1994(:,6);
eu_data= mat1994(:,7);
ee_data= mat1994(:,8);
q_data = mat1994(:,9);
h_data = mat1994(:,10);
%
% Log levels.
p = log(p_data);
u = log(u_data);
f = log(f_data);
eu= log(eu_data);
ee= log(ee_data/100);
q = log(q_data/100);
h = log(h_data/100);
MainDataLev = [p,u,f,q];   % for Figures, these are the only vars we need.
[TLev,vars] = size(MainDataLev);
%
% First differenced APL (others remain in log level).
p = log(p_data(2:end)./p_data(1:end-1));
u = log(u_data(2:end));
f = log(f_data(2:end));
eu= log(eu_data(2:end));
ee= log(ee_data(2:end)/100);
q = log(q_data(2:end)/100);
h = log(h_data(2:end)/100);
MainDataDiff = [p,u,f,q];   % for Figures, these are the only vars we need.
[TDiff,vars] = size(MainDataDiff);
%
%
% II.B) Set specification. 
% 
% First, the baseline. This is used for Fig. 9 and (some of) Fig. H.
lags = 4;
Tirf = 12;
trend = 0;          %=1 if detrend data (linear).
firstdiff = 0;      %=1 if APL is first-differenced.
uselagflows =0;     %=1 if first stage includes lagged vars other than lags of APL and U/L.
MainData = MainDataLev;
T = TLev;
%
% Run regressions, make IRFs, and save output for figures
irfregs_runols;
irfregs_makeirfs;
jf2mat = irf2(:,:,1);          % VAR system #1:  APL, U/L, and job finding rate 
qr2mat = irf2(:,:,2);          % VAR system #2:  APL, U/L, and JOLTS quit rate. 
fig9mat= [jf2mat, qr2mat(:,end)];
% NOTE: What we report in the paper for APL and U/L is from system #1. 
%       From system #2, we pull the IRF for the quit rate. From 
%       system #3 (below), we pull the IRF for chain length.
%
% Next, set specification for remainder of regressions in Fig. H:
uselagflows =1;
irfregs_runols;
irfregs_makeirfs;
jf2mat_ = irf2(:,:,1);         
qr2mat_ = irf2(:,:,2);          
figHmat= [jf2mat, jf2mat_, qr2mat, qr2mat_];
% NOTE: Fig H. reports APL and U/L for each VAR system and
%       for both the baseline and alternative models.  
%       Therefore, we save all output.
%
% Next, set specifications for regressions in Footnote 44. 
% The first of these uses 6 lags rather than 4. 
uselagflows =0;
lags =6;
irfregs_runols;
irfregs_makeirfs;
jf2mat = irf2(:,:,1);          
qr2mat = irf2(:,:,2);          
ft44_6lags_mat= [jf2mat, qr2mat(:,end)];
%
% The second enters APL in first differences rather than levels.
lags =4;
firstdiff =1;
MainData = MainDataDiff;
T = TDiff;
irfregs_runols;
irfregs_makeirfs;
jf2mat = irf2(:,:,1);          
qr2mat = irf2(:,:,2);          
ft44_diff_mat= [jf2mat, qr2mat(:,end)];
%
% The third uses an extended specification that includes 
% the job finding rate in every VAR system.
firstdiff =0;
MainData = MainDataLev;
T = TLev;
irfregs_runols;
irfregs_makeirfs;
qr3mat = irf3(:,:,1);   % Extended VAR system: APL, U/L, finding rate, quit rate.       
ft44_jfr_mat= qr3mat;



%
% III) Estimate 2001-2016.
%%%%%%%%%%%%%%%%%%%%%%%%%%
%      All regressions re-run over this shorter period.
%
% III.A) Format data:
date   = mat2001(:,1);
p_data = mat2001(:,2);
u_data = mat2001(:,3);
f_data = mat2001(:,6);
eu_data= mat2001(:,7);
ee_data= mat2001(:,8);
q_data = mat2001(:,9);
h_data = mat2001(:,10);
chain_data = mat2001(:, 11);
%
% Log levels.
p = log(p_data);
u = log(u_data);
f = log(f_data);
eu= log(eu_data);
ee= log(ee_data/100);
q = log(q_data/100);
h = log(h_data/100);
chain = log(chain_data);
MainDataLev = [p,u,f,q,chain];   % for Figures, these are the only vars we need.
[TLev,vars] = size(MainDataLev);
%
% First differenced APL (others remain in log level).
p = log(p_data(2:end)./p_data(1:end-1));
u = log(u_data(2:end));
f = log(f_data(2:end));
eu= log(eu_data(2:end));
ee= log(ee_data(2:end)/100);
q = log(q_data(2:end)/100);
h = log(h_data(2:end)/100);
chain = log(chain_data(2:end));
MainDataDiff = [p,u,f,q,chain];   % for Figures, these are the only vars we need.
[TDiff,vars] = size(MainDataDiff);
%
%
% III.B) Set specification. 
% 
% First, the baseline. This is used for Fig. 9 and (some of) Fig. H.
lags = 4;
Tirf = 12;
trend = 0;          %=1 if detrend data (linear).
firstdiff = 0;      %=1 if APL is first-differenced.
uselagflows =0;     %=1 if first stage includes lagged vars other than lags of APL and U/L.
MainData = MainDataLev;
T = TLev;
irfregs_runols;
irfregs_makeirfs;
ch2mat = irf2(:,:,3);          % VAR system #3:  APL, U/L, and chain length.
fig9mat= [fig9mat, ch2mat(:,end)];
% RECALL: We only pull the IRF for chain length from this system. 
%         This series is in the far-right column of ch2mat.
%
% Next, set specification for remainder of regressions in Fig. H:
uselagflows =1;
irfregs_runols;
irfregs_makeirfs;
ch2mat_ = irf2(:,:,3);         
figHmat= [figHmat, ch2mat, ch2mat_];
%
% Next, set specifications for regressions in Footnote 44. 
% The first of these uses 6 lags rather than 4. 
uselagflows =0;
lags =6;
irfregs_runols;
irfregs_makeirfs;
ch2mat = irf2(:,:,3);          
ft44_6lags_mat= [ft44_6lags_mat, ch2mat(:,end)];
%
% The second enters APL in first differences rather than levels.
lags =4;
firstdiff =1;
MainData = MainDataDiff;
T = TDiff;
irfregs_runols;
irfregs_makeirfs;
ch2mat = irf2(:,:,3);          
ft44_diff_mat= [ft44_diff_mat, ch2mat(:,end)];
%
% The third uses an extended regression model that includes 
% lags of both U/L and the JF rate in all of the second
% stage regressions.
firstdiff =0;
MainData = MainDataLev;
T = TLev;
irfregs_runols;
irfregs_makeirfs;
ch3mat = irf3(:,:,2);          
ft44_jfr_mat= [ft44_jfr_mat, ch3mat];      % Extended VAR system: APL, U/L, finding rate, chain length.  



%
% IV) Save Results to Excel. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
writematrix(fig9mat, '/home/Output/IRFs.xlsx', 'Sheet', 'Fig 9', 'Range', 'B3:F14');
writematrix(figHmat, '/home/Output/IRFs.xlsx', 'Sheet', 'Fig H1', 'Range', 'B3:S14');
writematrix(ft44_6lags_mat, '/home/Output/IRFs.xlsx', 'Sheet', 'Footnote 44 - Six lags', 'Range', 'B3:F14');
writematrix(ft44_diff_mat,  '/home/Output/IRFs.xlsx', 'Sheet', 'Footnote 44 - First diff APL', 'Range', 'B3:F14');
writematrix(ft44_jfr_mat,   '/home/Output/IRFs.xlsx', 'Sheet', 'Footnote 44 - Lags of JF rate', 'Range', 'B3:I14');
